import CONSTS from "./CONSTS";

/**
 * 百度地图 [Web]
 *
 * [BMap Demo](http://lbsyun.baidu.com/jsdemo.htm#c2_3)
 * [BMap JS API](http://lbsyun.baidu.com/cms/jsapi/reference/jsapi_reference.html#a3b5)
 *
 * @export
 * @class BaiduMap
 */
export default class BaiduMap {

  /**
   * 创建地图
   *
   * @static
   * @param {*} DOMString
   * @memberof BaiduMap
   */
  static CreateMap(DOMString, point) {
    let map = new BMap.Map(DOMString); //创建地图
    map.centerAndZoom(point, 14);

    return map;
  }

  /**
   * 获取地图坐标点
   *
   * @static
   * @param {*} { longitude, latitude}
   * @returns
   * @memberof BaiduMap
   */
  static GetPoint({
    longitude,
    latitude
  }) {
    return new BMap.Point(longitude, latitude);
  }

  /**
   * 获取文字标注
   *
   * @static
   * @param {string} [TextString='我是文字标注哦']
   * @returns
   * @memberof BaiduMap
   */
  static GetLabel(HtmlString = '附近有<span style="color: #40daa8;">没有</span>停车场') {
    let label = new BMap.Label(HtmlString, {
      offset: new BMap.Size(-30, -20)
    });
    label.setStyle({
      border: "1px solid #FEFEFE",
      padding: '2px 5px',
      borderRadius: '8px'
    });
    return label;
  }

  /**
   * 获取指定ICON
   *
   * @static
   * @param {*} [imageUrl=CONSTS.MAP.MARKER]
   * @returns
   * @memberof BaiduMap
   */
  static GetIcon(imageUrl = CONSTS.MAP.MARKER) {
    return new BMap.Icon(imageUrl, new BMap.Size(32, 32));
  }

  /**
   * 获取标识点
   *
   * @static
   * @param {*} point
   * @param {*} config
   * @returns
   * @memberof BaiduMap
   */
  static GetMarker(point, config) {
    const opts = Object.assign({}, config);
    return new BMap.Marker(point, opts);
  }

  static GetUserMarker(point) {
    const icon = this.GetIcon(CONSTS.MAP.USER);
    // const icon = this.GetIcon(CONSTS.MAP.MARKER);
    return this.GetMarker(point, {
      icon: icon
    });
  }

  static GetMaskMarker(point) {
    const icon = this.GetIcon(CONSTS.MAP.MASK);
    // const icon = this.GetIcon(CONSTS.MAP.MARKER);
    return this.GetMarker(point, {
      icon: icon
    });
  }

  static GetPMarker(point) {
    const icon = this.GetIcon(CONSTS.MAP.MARKER);
    return this.GetMarker(point, {
      icon: icon
    });
  }

  static CreateMarker(map, point) {
    if (map) {
      let pt = this.GetPoint(point);
      let marker = this.GetPMarker(pt);
      map.addOverlay(marker);

      return marker;
    }
  }

  /**
   * 获取地图圆的标识层
   *
   * @static
   * @memberof BaiduMap
   */
  static GetCircle(point) {
    return new BMap.Circle(point, 300, {
      strokeColor: "#40daa8",
      strokeWeight: 0.5,
      strokeOpacity: 0.8
    }); //创建圆
  }

  /**
   * 清除地图
   *
   * @static
   *
   * @memberOf BaiduMap
   */
  static Clear() {
    this.clearOverlays();
  }

  /**
   * 调用APP百度导航
   *
   * @static
   * @param {string} [point={ latitude: '', longitude: '' }]
   *
   * @memberOf BaiduMap
   */
  static Navigation(point = { latitude: '', longitude: '' }) {
    // 导航
    window.location.href =
    `bdapp://map/navi?location=${point.latitude},${point.longitude}&src=andr.baidu.openAPIdemo`;
  }

  /**
   * 初始化地图及相关的逻辑
   *
   * @static
   * @memberof BaiduMap
   */
  static InitMap(config = {
    id: 'allmap',
    title: '',
    isScale: false,
    point: {}
  }) {
    let pt = this.GetPoint(config.point);
    let map = this.CreateMap(config.id, pt);


    let userMaker = this.GetUserMarker(pt);
    map.addOverlay(userMaker);

    if (config.isScale) {
      let top_left_control = new BMap.ScaleControl({
        anchor: BMAP_ANCHOR_TOP_LEFT
      }); // 左上角，添加比例尺
      top_left_control.setOffset(new BMap.Size(10, 55));
      map.addControl(top_left_control);
    }

    // let userMaker = this.GetMaskMarker(pt);
    // if (config.title) {
    //     let userLabel = this.GetLabel(config.title);
    //     userMaker.setLabel(userLabel);
    // }
    // let userCircle = this.GetCircle(pt);
    // map.addOverlay(userMaker);
    // map.addOverlay(userCircle);

    return map;
  }
}
